import axios from "axios"
import { ElMessage } from 'element-plus'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getSign} from '@/utils/Sign'
// //接口
 export const WS = "ws://222.175.153.34:3100/jeecgboot/"
 // export const baseURL = "http://222.175.153.34:3100/jeecgboot"
 export const baseURL = "/api"

// export const WS = "ws://192.161.31.168:3100/jeecgboot/"
// export const baseURL = "http://192.168.31.161:3100/jeecgboot"

// export const WS = "ws://192.168.31.220:3100/jeecgboot/"//登录
// export const baseURL = "http://192.168.31.220:3100/jeecgboot"

// export const WS = "ws://192.168.31.152:3100/jeecgboot/"//登录
// export const baseURL = "http://192.168.31.152:3100/jeecgboot"
const request = axios.create({
    // API 请求的默认前缀
    baseURL: baseURL,
    timeout: 100000, // 请求超时时间
    headers: {
        'Content-Type':'application/json;charset=UTF-8',
         // 'X-Access-Token':getToken(),
        withCredentials: true
    },
})
request.interceptors.request.use(config => {
	let pianm=getSign(config.url, config.params)
	if (getToken('byj_Token')) {
	    const timestamp = Date.parse(new Date());
	   config.headers['X-Access-Token'] =getToken('byj_Token') // 让每个请求携带自定义token 请根据实际情况自行修改
	   config.headers['X-Timestamp'] = timestamp; // 让每个请求携带自定义token 请根据实际情况自行修改
	   config.headers['X-Tenant-Id'] =getToken('byj_Login'); // 让每个请求携带自定义token 请根据实际情况自行修改
	   config.headers['X-Sign'] = pianm; // 让每个请求携带自定义token 请根据实际情况自行修改
	}
    return config
}, {})
// request.interceptors.response.use((response) =>{
//     if(response.data.code != 200 && response.data.code != 1000 && response.data.code != 0){
//         ElMessage.error(response.data.msg)
//         return Promise.reject()
//     }
//     return response.data
// },{
// })
// 响应拦截器
request.interceptors.response.use(
    response => {
       // if(response.data.code != 200 && response.data.code != 1000 && response.data.code != 0){
       //     // ElMessage.error(response.data.msg)
		//    if(response.data.message == '验证码错误'||response.data.message == '该用户不存在，请注册'||response.data.message == '用户名或密码错误'){
		// 	   ElMessage.error(response.data.message)
		//    }else{
		// 	   // ElMessage.error('数据异常!')
		//    }
       //     return Promise.reject()
       // }
       return response.data;
    },
    error => {
      console.log(error.response.status)
	   if (error.response.status === 401) {
		    console.log('响应拦截器-错误-401')
			removeToken('byj_Token')
			removeToken('byj_Login')
			// window.open('http://222.175.153.34:3100/login',"_self")//1920
	   }
    }
)
// // 响应拦截器
// request.interceptors.response.use(function (response) {
//   console.log('响应拦截器', response)
//   return response
// }, async function (error) {
//   // 如果发生了错误，判断是否是401
//   console.dir(error)
//   console.dir(error.response.status)
//   if (error.response.status === 401) {
//     // 出现401就在这里面 开始处理 ---
//     console.log('响应拦截器-错误-401')
//     const refreshToken = store.state.tokenInfo.refresh_token
//     // if (有refresh_token) {       ---- 有refresh_token
//     if (refreshToken) {
      
//     } else {
  
//     }
//   } else {
//     return Promise.reject(error)
//   }
// })
// // 异常拦截处理器
const errorHandler = (error) => {
	console.log(error)
    return Promise.reject(error)
}
export default request;